import { hasRoute } from './utils'
let ve = __wxConfig.envVersion
let host = ''
switch (ve) {
  case 'release':
    host = 'https://www.6laoyou.com/api/'
    break
  case 'trial':
    host = 'https://www.6laoyou.com/api/'
    break
  default:
    host = 'https://www.6laoyou.com/api/'
}
module.exports.requestServerData = function (url, method, data = {}) {
  return new Promise(function (resolve, reject) {
    wx.request({
      url: host + url,
      data,
      method,
      header: {
        Authorization: 'Bearer ' + wx.getStorageSync('token'),
        'content-type': 'application/json;charset=UTF-8',
      },
      success(res) {
        if (res.data.message == '登录过期，请重新登录') {
          if (!hasRoute()) {
            wx.setStorageSync('token', '')
            wx.switchTab({
              url: '/pages/my/index',
            })
          }
        } else if (res.data.code == 200) {
          resolve(res.data.data)
        } else {
          wx.hideLoading()
          resolve(res.data.data)
        }
      },
      fail(res) {
        console.log(111)
        reject(res.data.data)
      },
    })
  })
}
module.exports.requestServerData1 = function (url, method, data = {}) {
  let currentRoute = getCurrentPages().route
  return new Promise(function (resolve, reject) {
    wx.request({
      url: host + url,
      data,
      method,
      header: {
        Authorization: 'Bearer ' + wx.getStorageSync('token'),
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      success(res) {
        if (res.data.message == '登录过期，请重新登录') {
          if (!hasRoute()) {
            wx.setStorageSync('token', '')
            wx.navigateTo({
              url: '/user/pages/login/index',
            })
          }
        } else if (res.data.code == 200) {
          resolve(res.data.data)
        } else {
          resolve(res.data)
        }
      },
      fail(res) {
        reject(res.data.data)
      },
    })
  })
}

export const getNowDate = ()=>{
  const now = new Date();

const year = now.getFullYear();
const month = ('0' + (now.getMonth() + 1)).slice(-2);
const day = ('0' + now.getDate()).slice(-2);
const hours = ('0' + now.getHours()).slice(-2);
const minutes = ('0' + now.getMinutes()).slice(-2);
const seconds = ('0' + now.getSeconds()).slice(-2);

const formattedTime = year + '-' + month + '-' + day ;
  return formattedTime
}